//
//  NetworkManager.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/1/23.
//

import Foundation
import Moya

class CommonNetManager {
    static let shared = CommonNetManager()
    
    private let provider: MoyaProvider<CommonApi>
    
    private init() {
        provider = MoyaProvider<CommonApi>()
    }
    //统一请求处理
    private func request<M: Codable>(_ target: CommonApi, modeType: M.Type) async throws-> M {
        try await withCheckedThrowingContinuation({ continuation in
            provider.request(target) { result in
                dealNetResult(result: result, modeType: modeType, filterCode: []) {
                    continuation.resume(returning: $0)
                } fail: {
                    continuation.resume(throwing: $0)
                }
            }
        })
    }
}

//接口请求
extension CommonNetManager {
    
    func get<M: Codable>(url: String,modeType: M.Type) async throws -> M {
        let api : CommonApi = .getConfig(url: url)
        return try await request(api, modeType: modeType)
    }
}
